var ctx = localStorage.getItem("ctx");
var wholeprovinceId = '';
var wholecityId = '';
var wholecompanyId = '';
var table = '';
var suvStatus = 0;
var searchexamname = '';
var searchexamdate = '';
$(function() {
	
	
	juicer.set({
	    'tag::operationOpen': '-~',
	    'tag::operationClose': '+',
	    'tag::interpolateOpen': '#-',
	    'tag::interpolateClose': '+',
	    'tag::noneencodeOpen': '##-',
	    'tag::noneencodeClose': '+',
	    'tag::commentOpen': '-*',
	    'tag::commentClose': '+'
	});
	
	var uvId = '';
	
	
	var isAdmin = localStorage.getItem('isAdmin');
	console.log(isAdmin);
	if(isAdmin == 'false') {
		$(".isAdminProvince").hide();
		$(".isAdminCity").hide();
		$(".isAdminCompany").hide();
		//非超级管理员获取公司列表
		getCompanyByOtherUser();
	}
	if(isAdmin == 'true') {
		$(".isOtherUserCompany").hide();
	}
	
	//获取审核列表表格
	examTable(wholeprovinceId,wholecityId,wholecompanyId,suvStatus,searchexamname,searchexamdate);
	
	
		
	//获取省份
	getProvince();
	getCity('');
	getCompany('','');
	
	//根据选取的省份获取城市和公司
	$(".provinceSelect").on("change", function(){
		var provinceId = $(this).attr("value");
		getCity(provinceId);
		getCompany(provinceId,'');
	});
	
	//根据省份和城市获取公司
	$(".citySelect").on("change", function() {   	
		var provinceId = $(".provinceSelect").find("option:selected").attr("value");
		var cityId = $(this).attr("value");
		getCompany(provinceId,cityId);
	});
	
	
	//查询
	$(".btn-search").on("click", function() {
		wholeprovinceId = $(".provinceSelect").find("option:selected").attr("value");
		wholecityId = $(".citySelect").find("option:selected").attr("value");
		//判断是否是超级管理员
    	if(isAdmin == 'false') {
    		wholecompanyId = $(".otherUserCompanySelect").find("option:selected").attr("value");
    	}
    	else {
    		wholecompanyId = $(".companySelect").find("option:selected").attr("value");
    	}
//		wholecompanyId = $(".companySelect").find("option:selected").attr("value");
		suvStatus = $(".statusSelect").find("option:selected").attr("value");
		searchexamname = $(".searchexamname").val();
		searchexamdate = $(".searchexamdate").val();
		table.destroy();
		
		//查询获取审核列表表格
		examTable(wholeprovinceId,wholecityId,wholecompanyId,suvStatus,searchexamname,searchexamdate);
		
		
	});
	
	
	
	
	
	
	//查看详情
	$(document).on( 'click', '.btn-detail', function () {
		
	    var data = table.row( $(this).parents('tr') ).data();
	    var userId = data.userId;
	    console.log(userId);
	    $.ajax({
	    	
	    	type: 'POST',
			url: ctx+'/driver/v1/dvdetail',
			dataType: 'json',
			data: {
				suid: userId
			},
			success: function(data) {
				console.log(data);
				var authType = data.authType;
				//3:出租车  2:专车
				if(authType == 2) {
					$(".workLicenseNumberTh").hide();
					$(".workLicenseNumberTd").hide();
					$(".workLicenseImageDiv").hide();
					$(".workLicenseImageDiv").hide();
				}
				if(authType == 3) {
					$(".workLicenseNumberTh").show();
					$(".workLicenseNumberTd").show();
					$(".workLicenseNumberTd").html(data.workLicenseNumber);
					$(".workLicenseImageDiv").show();
					$(".workLicenseImage").attr("src", data.workLicenseImage);
				}
				uvId = data.uvId;
				$(".provinceTd").html(data.provice_name);
		        $(".cityTd").html(data.cityName);
		        $(".companyTd").html(data.organizationName);
		        $(".nameTd").html(data.trueName);
		        var examsex = data.sex;
		        if(examsex == 1) {
		        	$(".sexTd").html("男");
		        }
		        else if(examsex == 2) {
		        	$(".sexTd").html("女");
		        }
		        else {
		        	$(".sexTd").html(data.sex);
		        }
		        $(".telTd").html(data.telPhone);
		        $(".idCardTd").html(data.idCard);
		        $(".firstLicDateTd").html(data.jszLssueDate.substring(0,10));
		        $(".endLicDateTd").html(data.jszValidityPeriod.substring(0,10));
		        $(".yearsTd").html(data.driving);
		        $(".licTypeTd").html(data.permittedType);
		        $(".LicNoTd").html(data.jszFileNumber);
		        $(".plateTd").html(data.lisencePlate);
		        $(".carTypeTd").html(data.vehicleModel);
		        $(".vehicleNoTd").html(data.vehicleNumber);
		        $(".carNameTd").html(data.vehicleOwner);
		        $(".registerDateTd").html(data.xszLssueDate.substring(0,10));
		        
		        //图片
		        $(".idCardPositiveImage").attr("src", data.idCardPositiveImage);
		        $(".idCardOppositeImage").attr("src", data.idCardOppositeImage);
		        $(".vehicleImage").attr("src", data.vehicleImage);
		        $(".jszImage").attr("src", data.jszImage);
		        $(".jszCopyImage").attr("src", data.jszCopyImage);
		        $(".xszImage").attr("src", data.xszImage);
		        $(".xszCopyImage").attr("src", data.xszCopyImage);
		        
		        $("#myModalDetail").modal('show');
			}
	    	
	    });
	    
	} );
	
	
	//页面加载完毕渲染未审核通过原因列表
	$.ajax({
		type: 'POST',
		url: ctx+'/driver/manage/v2/no/audit',
		dataType: 'json',
		success: function(data) {
			console.log(data);
			var examNoPassData = {};
			examNoPassData.list = data;
			var examNoPassTpl = document.getElementById("examNoPassDivTpl").innerHTML;
			var examNoPasshtml = juicer(examNoPassTpl, examNoPassData);
			$(".examNoPassDiv").html(examNoPasshtml);
		}
	});
	

	
	//审核通过操作
	$(".exam-success-btn").on("click", function() {

		$("#modal1").modal('show');
		
	});
	//审核通过确认操作
	$(".carstyle-btn-confirm").on("click", function() {
		
		var vehicleLevelId = $(".carStyleSelect").find("option:selected").attr("value"); 
//		alert(vehicleLevelId);
		$.ajax({
			type: 'POST',
			url: ctx+'/driver/manage/v1/audit',
			dataType: 'json',
			data: {
				suvStatus: 2,
				vehicleLevelId: vehicleLevelId,
				uvId: uvId
			},
			success: function(data) {
				//审核通过相关操作
				window.location.reload();
			}
		});
		
	});
	
	//审核未通过显示未通过原因列表模态框
	$(".exam-failed-btn").on("click", function() {
		
		$("#modal2").modal('show');
		
	});
	
	//点击未审核通过原因模态框确认按钮
	$('.btn-confirm').on('click', function() {
		var vrIds = '';
		$(".examNoPassDiv").find('input').each(function() {
			if($(this).is(':checked')) {
				vrIds += $(this).attr('data-value') + ',';
			}
		});
		if(vrIds == '') {
			alert('请选择原因');
		}
		else {
			$.ajax({
				type: 'POST',
				url: ctx+'/driver/manage/v1/audit',
				dataType: 'json',
				data: {
					suvStatus: 3,
					uvId: uvId,
					vrid: vrIds
				},
				success: function(data) {
					console.log(data+'操作成功');
					window.location.reload();
				}
			});
		}
	});
	
	
	//点击详情模态框图片查看原图
	$('#myModalDetail img').on('click', function() {
		console.log($(this).attr('src'));
		var bigSrc = $(this).attr('src');
		var bigTitle = $(this).siblings('h4').text();
		$('.bgmodal img').attr('src',bigSrc);
		$('.bg').fadeIn(200);
		$('.bgmodal').fadeIn(400);
	});
	$('.bg').on('click', function() {
		$('.bg').fadeOut(200);
		$('.bgmodal').fadeOut(200);
	});
	
	//点击旋转图片
	var current = 0;
	$('.rotate-icon').on('click', function() {
//		alert($(this).siblings("img").attr('src'));
		var roteteImgSrc = $(this).siblings("img").attr('src');
		current = (current+90)%360;
		$(this).siblings("img").css({'transform': 'rotate('+current+'deg)'});
	});
	$('.bgmodal img').on('click', function() {
//		alert($(this).siblings("img").attr('src'));
//		var roteteImgSrc = $(this).siblings("img").attr('src');
		current = (current+90)%360;
		$(this).css({'transform': 'rotate('+current+'deg)'});
	});
	
	
	
	//获取省份
    function getProvince() {
    	$.ajax({
        	type: 'POST',
    		url: ctx+'/proandcity/v1/pro',
    		dataType: 'json',
    		success: function(data) {
    			
    			var provinceTpl = document.getElementById("provinceSelectTpl").innerHTML;
    			var provincehtml = juicer(provinceTpl, data);
    			var newprovincehtml = "<option value=''>全部</option>" + provincehtml;
    			$(".provinceSelect").html(newprovincehtml);
    			
    		}
    	});
    }
    
	
    //根据省份获取城市(可以传空，代表全部)
    function getCity(provinceId) {
    	$.ajax({
        	type: 'POST',
    		url: ctx+'/proandcity/v1/city',
    		dataType: 'json',
    		data: {
    			proviceId: provinceId
    		},
    		success: function(data) {
    			var cityTpl = document.getElementById("citySelectTpl").innerHTML;
    			var cityhtml = juicer(cityTpl, data);
    			var newcityhtml = "<option value=''>全部</option>" + cityhtml;
    			$(".citySelect").html(newcityhtml);
    			
    		}
    	});
    }
    
    
    //根据省份和城市获取公司(可以传空，代表全部)
    function getCompany(provinceId,cityId) {
    	$.ajax({
    		type: 'POST',
    		url: ctx+'/sys/v1/organization/listId',
    		dataType: 'json',
    		data: {
    			proviceId: provinceId,
    			sysCityId: cityId
    		},
    		success: function(data) {
    			var company = {};
    			company.list = data;
    			var companyTpl = document.getElementById("companySelectTpl").innerHTML;
    			var companyhtml = juicer(companyTpl, company);
    			var newcompanyhtml = "<option value=''>全部</option>" + companyhtml;
    			$(".companySelect").html(newcompanyhtml);
    		}
    	});
    }
    
    //获取非超级管理员用户所能访问的公司列表
    function getCompanyByOtherUser() {
    	$.ajax({
    		type: 'POST',
    		url: ctx+'/sys/v1/organization/organizationList',
    		dataType: 'json',
    		success: function(data) {
    			console.log(data);
    			var company = {};
    			company.list = data;
    			var companyTpl = document.getElementById("otherUserCompanySelectTpl").innerHTML;
    			var companyhtml = juicer(companyTpl, company);
    			var newcompanyhtml = "<option value=''>全部</option>" + companyhtml;
    			$(".otherUserCompanySelect").html(newcompanyhtml);
    		}
    	});
    }
	
	
});
